package com.wust.footprint;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.amo.demo.wheelview.NumericWheelAdapter;
import com.amo.demo.wheelview.OnWheelChangedListener;
import com.amo.demo.wheelview.WheelView;
import com.wust.R;
import com.wust.baby.Baby;
import com.wust.baby.BabyDao;
import com.wust.baby.BabyInfo;
import com.wust.baby.BabyInfoDao;
import com.wust.user.User;
import com.wust.user.UserDao;
import com.wust.util.SqltimeToTime;


/*
一：主布局界面
二：点击控件触发事件后效果图
三：拍照完之后效果图
四：裁剪界面效果图
五：点击相册后返回的图片效果图
六：裁剪完从相册PICK的保存后的效果图




*/

/**
 * @Title: PicCutDemoActivity.java
 * @Package com.xiaoma.piccut.demo
 * @Description: 图片裁剪功能测试
 * @author XiaoMa
 */
public class PicCutDemoActivity extends Activity {


	private String tp = null;
	private ImageButton entrybtn;
	private String user_name;
	private String iconPath;
	private String tempPath;
	private EditText nameText;
	private RadioGroup babySex;
	private String sex="男";
	private Button record_date=null;
	private Button record_height=null;
	private Button record_weight=null;
	private Button record_chest=null;
	private Button record_headwidth=null;
	private String heightstring,weigthstring,buststring,circumferencestring;
	private SqltimeToTime timeTransfer;
	private static int START_YEAR = 1990, END_YEAR = 2100;
	private Dialog dialog;
	private String baby_date;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.baby_info_set);
		nameText=(EditText) findViewById(R.id.baby_set_nickname);
		babySex=(RadioGroup) findViewById(R.id.sex_radioGroup);
		timeTransfer=new SqltimeToTime();
		record_height=(Button)findViewById(R.id.baby_set_height2);
		record_weight=(Button)findViewById(R.id.baby_set_weight2);
		 record_date=(Button)findViewById(R.id.baby_set_date2);
		record_chest=(Button)findViewById(R.id.baby_set_chest2);
		record_headwidth=(Button)findViewById(R.id.baby_set_headwidth2);
		babySex.setOnCheckedChangeListener(new SexChangeListener());
		Intent intent=getIntent();
		 user_name=intent.getStringExtra("user_name");
		 iconPath=Environment.getExternalStorageDirectory()
					+ "/daying/"+user_name+".jpg";
		tempPath=Environment.getExternalStorageDirectory()
				+ "/daying/"+"temp.jpg";
		 File file=new File(Environment.getExternalStorageDirectory()
					+ "/daying/");
		 if(!file.isDirectory())
		 {
			 System.out.println("create dir");
			 file.mkdir();
		 }
		entrybtn=(ImageButton)findViewById(R.id.regist_title_ok);
		entrybtn.setOnClickListener(new EntryListener());
		 record_date.setOnClickListener(new OnClickListener() {
				
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showDateTimePicker();
				}
			});
	        record_height.setOnClickListener(new OnClickListener() {
				
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showHeightPicker();
				}
			});
	        record_weight.setOnClickListener(new OnClickListener() {
				
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showWeightPicker();
				}
			});
	        record_chest.setOnClickListener(new OnClickListener() {
				
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showChestPicker();
				}
			});
	        record_headwidth.setOnClickListener(new OnClickListener() {
				
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showHeadWidthPicker();
				}
			});
		//初始化
		
	}
/** 弹出身高选择器*/
	
	private void showHeightPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择身高");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.height_layout, null);
		
		final WheelView wv_height_z = (WheelView) view.findViewById(R.id.height_z);
		wv_height_z.setAdapter(new NumericWheelAdapter(0, 150));
		wv_height_z.setCyclic(true);
		wv_height_z.setCurrentItem(40);
		
		final WheelView wv_height_x=(WheelView)view.findViewById(R.id.height_x);
		wv_height_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_height_x.setCyclic(true);
		wv_height_x.setCurrentItem(00);
		wv_height_x.setLabel("  cm");
		
		int textsize=25;
		wv_height_z.TEXT_SIZE=textsize;
		wv_height_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_height_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_height_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);
				heightstring=wv_height_z.getCurrentItem()+"."+decimal.format(wv_height_x.getCurrentItem());
				record_height.setText(heightstring+"cm");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/** 体重选择器*/
	private void showWeightPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择体重");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.weight_layout, null);
		
		final WheelView wv_weight_z = (WheelView) view.findViewById(R.id.weight_z);
		wv_weight_z.setAdapter(new NumericWheelAdapter(0, 60));
		wv_weight_z.setCyclic(true);
		wv_weight_z.setCurrentItem(2);
		
		final WheelView wv_weight_x=(WheelView)view.findViewById(R.id.weight_x);
		wv_weight_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_weight_x.setCyclic(true);
		wv_weight_x.setCurrentItem(55);
		wv_weight_x.setLabel("  kg");
		
		int textsize=25;
		wv_weight_z.TEXT_SIZE=textsize;
		wv_weight_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_weight_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_weight_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);	
				weigthstring=wv_weight_z.getCurrentItem()+"."+decimal.format(wv_weight_x.getCurrentItem());
				record_weight.setText(weigthstring+"kg");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/** 胸围选择器*/
	private void showChestPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择胸围");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.chest_layout, null);
		
		final WheelView wv_chest_z = (WheelView) view.findViewById(R.id.chest_z);
		wv_chest_z.setAdapter(new NumericWheelAdapter(0, 60));
		wv_chest_z.setCyclic(true);
		wv_chest_z.setCurrentItem(49);
		
		final WheelView wv_chest_x=(WheelView)view.findViewById(R.id.chest_x);
		wv_chest_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_chest_x.setCyclic(true);
		wv_chest_x.setCurrentItem(43);
		wv_chest_x.setLabel("  cm");
		
		int textsize=25;
		wv_chest_z.TEXT_SIZE=textsize;
		wv_chest_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_chest_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_chest_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);	
				buststring=wv_chest_z.getCurrentItem()+"."+decimal.format(wv_chest_x.getCurrentItem());
				record_chest.setText(buststring+"cm");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/** 头围选择器*/
	private void showHeadWidthPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择头围");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.head_layout, null);
		
		final WheelView wv_headwidth_z = (WheelView) view.findViewById(R.id.headwidth_z);
		wv_headwidth_z.setAdapter(new NumericWheelAdapter(0, 60));
		wv_headwidth_z.setCyclic(true);
		wv_headwidth_z.setCurrentItem(49);
		
		final WheelView wv_headwidth_x=(WheelView)view.findViewById(R.id.headwidth_x);
		wv_headwidth_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_headwidth_x.setCyclic(true);
		wv_headwidth_x.setCurrentItem(43);
		wv_headwidth_x.setLabel("  cm");
		
		int textsize=25;
		wv_headwidth_z.TEXT_SIZE=textsize;
		wv_headwidth_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_headwidth_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_headwidth_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);	
				circumferencestring=wv_headwidth_z.getCurrentItem()+"."+decimal.format(wv_headwidth_x.getCurrentItem());
				record_headwidth.setText(circumferencestring+"cm");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	private void showDateTimePicker() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		

		// 添加大小月月份并将其转换为list,方便之后的判断
		String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
		String[] months_little = { "4", "6", "9", "11" };

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);

		dialog = new Dialog(this);
		dialog.setTitle("请选择日期与时间");
		// 找到dialog的布局文件
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.time_layout, null);

		// 年
		final WheelView wv_year = (WheelView) view.findViewById(R.id.year);
		wv_year.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
		wv_year.setCyclic(true);// 可循环滚动
		wv_year.setLabel("年");// 添加文字
		wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		// 月
		final WheelView wv_month = (WheelView) view.findViewById(R.id.month);
		wv_month.setAdapter(new NumericWheelAdapter(1, 12));
		wv_month.setCyclic(true);
		wv_month.setLabel("月");
		wv_month.setCurrentItem(month);

		// 日
		final WheelView wv_day = (WheelView) view.findViewById(R.id.day);
		wv_day.setCyclic(true);
		// 判断大小月及是否闰年,用来确定"日"的数据
		if (list_big.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 30));
		} else {
			// 闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new NumericWheelAdapter(1, 29));
			else
				wv_day.setAdapter(new NumericWheelAdapter(1, 28));
		}
		wv_day.setLabel("日");
		wv_day.setCurrentItem(day - 1);

		

		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String
						.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		// 根据屏幕密度来指定选择器字体的大小
		int textSize = 0;

		textSize = 20;

		wv_day.TEXT_SIZE = textSize;
		wv_month.TEXT_SIZE = textSize;
		wv_year.TEXT_SIZE = textSize;

		Button btn_sure = (Button) view.findViewById(R.id.btn_datetime_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_datetime_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);
				baby_date=(wv_year.getCurrentItem() + START_YEAR) + "-"
						 + decimal.format((wv_month.getCurrentItem() + 1)) + "-"
						 + decimal.format((wv_day.getCurrentItem() + 1));
				// 设置日期的显示
				 record_date.setText(baby_date);
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	class SexChangeListener implements OnCheckedChangeListener
	{

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if(group.getCheckedRadioButtonId()==R.id.radioButtonBoy)
				sex="男";
			else
				sex="女";
		}
		
	}
	/**
	 * 初始化方法实现
	 */
	class EntryListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Baby baby=new Baby();
			BabyDao babyDao=new BabyDao(PicCutDemoActivity.this);
			BabyInfoDao infoDao=new BabyInfoDao(PicCutDemoActivity.this);
			BabyInfo info=new BabyInfo();
			 UserDao userDao=new UserDao(PicCutDemoActivity.this);
			 User user=userDao.getUserByName(user_name);
			
			 if(nameText.getText().toString()==null)
				 Toast.makeText(PicCutDemoActivity.this, "昵称不能为空", Toast.LENGTH_SHORT).show();
			 else if(record_weight.getText().toString()==null)
				 Toast.makeText(PicCutDemoActivity.this, "体重不能为空", Toast.LENGTH_SHORT).show();
			 else if(record_height.getText().toString()==null)
				 Toast.makeText(PicCutDemoActivity.this, "身高不能为空", Toast.LENGTH_SHORT).show();
			 else if(record_chest.getText().toString()==null)
				 Toast.makeText(PicCutDemoActivity.this, "胸围不能为空", Toast.LENGTH_SHORT).show();
			 else if(record_headwidth.getText().toString()==null)
				 Toast.makeText(PicCutDemoActivity.this, "头围不能为空", Toast.LENGTH_SHORT).show();
			 else if(record_date.getText().toString()==null)
				 Toast.makeText(PicCutDemoActivity.this, "出生日期不能为空", Toast.LENGTH_SHORT).show();
			 else
			 {
				String date=timeTransfer.dateToString(new Date(), "yyyy-MM-dd");
				 baby.setBaby_icon(iconPath);
				 baby.setUser_name(user.getUser_name());
				 baby.setBaby_name(nameText.getText().toString());
				 baby.setBaby_date(baby_date);
				 baby.setBaby_sex(sex);
				 System.out.println("sex:"+sex);
				 double weigth=Double.parseDouble(weigthstring);
				 double height=Double.parseDouble(heightstring);
				 
				 babyDao.insertBaby(baby);
				Baby temp=babyDao.getBabyByName(nameText.getText().toString(),user_name);
				 System.out.println("id"+temp.getBaby_id());
				 info.setInfo_height(height);
				 info.setInfo_weigth(weigth);
				 info.setBaby_id(temp.getBaby_id());
				 info.setInfo_date(baby_date);
				 info.setInfo_bust(Double.parseDouble(buststring));
					info.setInfo_circumference(Double.parseDouble(circumferencestring));
				 infoDao.insertBabyInfo(info);
				 Intent intent=new Intent(PicCutDemoActivity.this,LoginActivity.class);
				 intent.putExtra("user_id", user.getUser_id());
				 startActivity(intent);
				 finish();
			 }
		}
		
	}
	
}